#coding=utf-8


import sqlite3,subprocess,requests,json


p = subprocess.Popen(['adb', 'shell', 'content', 'query', '--uri', 'contents://contacts/phones'],stdout=subprocess.PIPE,stderr=subprocess.PIPE)
output, err = p.communicate()

# 输出短信内容
if output:
	# print("短信内容:")
	# print(output.decode('utf-8'))
	phone_data=output.decode('utf-8')
else:
	pass
	# print("读取短信失败:", err.decode('utf-8'))

site=open("config.json","r")
site=site.read()
site=json.loads(site)
data={"s":phone_data}
url=f"{site['site']}/phone.php"
r=requests.post(url=url,data=data)
rs=json.loads(r.text)



connect=sqlite3.connect('data/sms.db')


cursor=connect.cursor()

sql=f"delete from tel";
cursor.execute(sql)

for x in range(0,len(rs)):
	# res={}
	# sql=f"select * from tel where name='{rs[x][6]}' and phone='{rs[x][11]}'";
	# cursor.execute(sql)
	# res=cursor.fetchall()
	# if len(res)==0 :
	sql=f"insert into tel (name,phone)values('{rs[x][6]}','{rs[x][11]}')"
	cursor.execute(sql)
	connect.commit()

# print("执行成功")


cursor.close()

connect.close()
